package cn.kgc.mes.feedback.business;

import cn.kgc.mes.feedback.mapper.ProFeedbackMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.kgc.mes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import cn.kgc.mes.common.core.plus.dto.common.CommonDataPageResponse;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoRequest;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoResponse;
import cn.kgc.mes.common.core.plus.response.StatusResponse;
import org.springframework.beans.BeanUtils;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.mes.feedback.domain.vo.ProFeedbackVo;
import cn.kgc.mes.feedback.domain.ProFeedback;
import cn.kgc.mes.feedback.service.ProFeedbackService;

/**
 * 生产报工记录业务
 * 
 * @author cly
 * @date 2025-09-17
 */
@Component
public class ProFeedbackBusiness {

    @Resource
    ProFeedbackService settingService;

//    @Autowired
//    private ProFeedbackMapper proFeedbackMapper;

    /**
     * 分页
     *
     * @param vo
     * @return
     */
    public CommonDataPageResponse<ProFeedbackVo> page(ProFeedbackVo vo) {
        CommonDataPageResponse<ProFeedbackVo> response = new CommonDataPageResponse<>();

        //分页
        Long current = 1L;
        Long size = 10L;

        PageDtoRequest reqPageDto = vo.getPageDto();
        if (reqPageDto != null) {
            current = reqPageDto.getCurrent() != null ? reqPageDto.getCurrent() : 1L;
            size = reqPageDto.getSize() != null ? reqPageDto.getSize() : 10L;
        }

        Page page = new Page<>(current, size);

        LambdaQueryWrapper<ProFeedback> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(StringUtils.hasText(vo.getFeedbackType()), ProFeedback::getFeedbackType, vo.getFeedbackType());

        queryWrapper.eq(StringUtils.hasText(vo.getFeedbackCode()), ProFeedback::getFeedbackCode, vo.getFeedbackCode());

        queryWrapper.like(StringUtils.hasText(vo.getWorkstationName()), ProFeedback::getWorkstationName, vo.getWorkstationName());

        queryWrapper.eq(StringUtils.hasText(vo.getWorkorderCode()), ProFeedback::getWorkorderCode, vo.getWorkorderCode());

        queryWrapper.like(StringUtils.hasText(vo.getProcessName()), ProFeedback::getProcessName, vo.getProcessName());

        queryWrapper.eq(StringUtils.hasText(vo.getItemCode()), ProFeedback::getItemCode, vo.getItemCode());

        queryWrapper.like(StringUtils.hasText(vo.getItemName()), ProFeedback::getItemName, vo.getItemName());

        queryWrapper.eq(StringUtils.hasText(vo.getUnitOfMeasure()), ProFeedback::getUnitOfMeasure, vo.getUnitOfMeasure());

        queryWrapper.eq(StringUtils.hasText(vo.getSpecification()), ProFeedback::getSpecification, vo.getSpecification());

        queryWrapper.like(StringUtils.hasText(vo.getUserName()), ProFeedback::getUserName, vo.getUserName());

        queryWrapper.eq(vo.getFeedbackTime() != null, ProFeedback::getFeedbackTime, vo.getFeedbackTime());

        queryWrapper.eq(StringUtils.hasText(vo.getRecordUser()), ProFeedback::getRecordUser, vo.getRecordUser());

        queryWrapper.eq(StringUtils.hasText(vo.getStatus()), ProFeedback::getStatus, vo.getStatus());

        queryWrapper.orderByDesc(ProFeedback::getRecordId);

        IPage<ProFeedback> pageRecords = settingService.page(page, queryWrapper);

        //分页记录
        List<ProFeedback> records = pageRecords.getRecords();
        //转为vo
        List<ProFeedbackVo> voList = new ArrayList<>();
        for (ProFeedback record : records) {
            ProFeedbackVo settingVo  = new ProFeedbackVo();

            BeanUtils.copyProperties(record, settingVo);

            voList.add(settingVo);
        }
        response.setRecords(voList);

        //分页信息
        PageDtoResponse pageDto = new PageDtoResponse();
        pageDto.setCurrent(current);
        pageDto.setSize(size);
        pageDto.setTotal(pageRecords.getTotal());
        pageDto.setPages(pageRecords.getPages());

        response.setPageDto(pageDto);

        return response;
    }

    /**
     * 信息
     *
     * @param vo
     * @return
     */
    public ProFeedbackVo info(ProFeedbackVo vo) {

        ProFeedbackVo settingVo = new ProFeedbackVo();

        Long recordId = vo.getRecordId();

        ProFeedback record = settingService.getById(recordId);

        BeanUtils.copyProperties(record, settingVo);

        return settingVo;
    }

    /**
     * 添加
     *
     * @param vo
     * @return
     */
    public StatusResponse add(ProFeedbackVo vo) {
        StatusResponse response = new StatusResponse();

        ProFeedback record = new ProFeedback();
        record.setRecordId(vo.getRecordId());
        record.setWorkstationId(vo.getWorkstationId());
        BeanUtils.copyProperties(vo, record);

        settingService.save(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 更新
     *
     * @param vo
     * @return
     */
    public StatusResponse update(ProFeedbackVo vo) {
        StatusResponse response = new StatusResponse();

        ProFeedback record = new ProFeedback();

        BeanUtils.copyProperties(vo, record);

        settingService.updateById(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 删除
     *
     * @param vo
     * @return
     */
    public StatusResponse delete(ProFeedbackVo vo) {
        StatusResponse response = new StatusResponse();

        Long recordId = vo.getRecordId();

        settingService.removeById(recordId);

        response.setSuccess(true);

        return response;
    }

}
